package com.joshluisaac.collections;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.joshluisaac.dtos.Customer;

public class SetDemo {

	public static void main(String[] args) {
		displayCustomerSet(buildCustomerSet());
	}

	public static void displayCustomerSet(Set<Customer> buildCustomerSet) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		for (Customer customer : buildCustomerSet) {
			System.out.println("Customer Id: " + customer.getId());
			System.out.println("Customer Name: " + customer.getName());

		}

		map.put("Customers", buildCustomerSet);
		ObjectMapper mapper = new ObjectMapper();
		try {
			mapper.defaultPrettyPrintingWriter().writeValue(
					new File("customers.json"), map);
			System.out.println(mapper.defaultPrettyPrintingWriter()
					.writeValueAsString(map));

		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static Set<Customer> buildCustomerSet() {
		Set<Customer> customers = new HashSet<Customer>();
		Customer customer1 = new Customer();
		customer1.setId(1);
		customer1.setName("John1");
		// System.out.println(customer1.toString());

		Customer customer2 = new Customer();
		customer2.setId(2);
		customer2.setName("John2");
		// System.out.println(customer2.toString());

		Customer customer3 = new Customer();
		customer3.setId(3);
		customer3.setName("John3");
		// System.out.println(customer3.toString());

		Customer customer4 = new Customer();
		customer4.setId(3);
		customer4.setName("John4");
		// System.out.println(customer4.toString());

		customers.add(customer1);
		customers.add(customer2);
		customers.add(customer3);
		customers.add(customer4);

		return customers;

	}
}
